package cn.xiongxf.org.service.impl;

import cn.xiongxf.basic.service.impl.BaseServiceImpl;
import cn.xiongxf.basic.util.Md5Utils;
import cn.xiongxf.basic.util.StrUtils;
import cn.xiongxf.org.domain.Employee;
import cn.xiongxf.org.mapper.EmployeeMapper;
import cn.xiongxf.org.service.EmployeeService;
import cn.xiongxf.user.domain.Logininfo;
import cn.xiongxf.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService {
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Transactional
    @Override
    public void add(Employee employee) {
        // 获取并设置盐值、密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        // 保存t_logininfo
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.save(logininfo);

        // 保存t_employee
        employee.setLogininfo_id(logininfo.getId());
        super.add(employee);

        // 保存t_employee_role中间表（员工-角色当做一对多处理）
        if (employee.getRole_id() != null){
            employeeMapper.saveEmployeeRole(employee);
        }


    }

    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);  // 0-员工
        return logininfo;
    }

    @Transactional
    @Override
    public void update(Employee employee) {
        // 获取并设置盐值、密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        // 登录信息id和员工id不能修改
        super.update(employee);
        // 修改中间表 = 除旧 + 迎新
        employeeMapper.removeEmployeeRoleByEmployeeId(employee.getId());
        if (employee.getRole_id() != null){
            employeeMapper.saveEmployeeRole(employee);
        }

        // 修改t_logininfo
        Employee oldEmployee = employeeMapper.loadById(employee.getId());
        Logininfo oldLogininfo = logininfoMapper.loadById(oldEmployee.getLogininfo_id());
        BeanUtils.copyProperties(employee,oldLogininfo);
        logininfoMapper.update(oldLogininfo);
    }

    @Transactional
    @Override
    public void del(Long id) {
        // 获取employee
        Employee employee = employeeMapper.loadById(id);
        // 删除logininfo
        logininfoMapper.remove(employee.getLogininfo_id());
        // 先删除t_employee_role中间表
        employeeMapper.removeEmployeeRoleByEmployeeId(id);
        // 再删除employee
        super.del(id);
    }

    @Override
    public List<Employee> findByShopId(Long id) {
        return employeeMapper.findByShopId(id) ;
    }
}
